/*
** 作者：TheBadZhang
** 创建时间：200316
** 最后修改时间：看文件
** 文件名：window.hpp
** 用途：窗口相关函数，创建与删除
*/
#ifndef ifWindowIsDefine
#define ifWindowIsDefine
#pragma once

#include "xege.h"
extern char          title[];
extern SDL_Window*   window;
extern SDL_Event*    winEvent;
extern SDL_Renderer* winRenderer;
extern SDL_Surface*  winSurface;
extern SDL_Texture*  winTexture;
extern color_t       winBGColor;
extern color_t       winFGColor;


// 设置标题（需要在 initGraph 之前使用）
void setTitle (char* t) {
    title = t;
}
// 初始化窗口，宽，高，模式（暂时禁用）
int initGraph (int w, int h, const int mode = WINDOW_DEFAULT) {
    // 宽度，高度，模式
    // 模式选项包括


    int winFlag = 0;
    IMG_Init (allImageType);
    
    switch (mode) {
        WINDOW_DEFAULT: winFlag = SDL_WINDOW_SHOWN; break;
        WINDOW_RESIZABLE: winFlag = SDL_WINDOW_RESIZABLE; break;
        default: winFlag = SDL_WINDOW_SHOWN; break;
    }
    SDL_Init (SDL_INIT_EVERYTHING);
    window = SDL_CreateWindow (title.c_str (), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, w, h, winFlag);
    // 创建窗口
    if (window == nullptr) {
        std::cerr << "CreateWindow Fail: " << SDL_GetError ();
        // 输出错误信息
        closeGraph ();
        // 退出
    } else {
        // atexit (closeGraph);
        // atexit(SDL_Quit);// 注册SDL_Quit，当退出时调用，使得退出时程序自动清理
        winRenderer = SDL_CreateRenderer (window, -1, rendererFlag);
        // 获取 window 的 render
        // winRenderer = SDL_CreateRenderer (window, -1, SDL_RENDERER_ACCELERATED);
        // winRenderer = SDL_CreateRenderer (window, -1, SDL_RENDERER_PRESENTVSYNC);
        // winRenderer = SDL_CreateRenderer (window, -1, SDL_RENDERER_TARGETTEXTURE);
        // setColor (winFGColor);
        winSurface = SDL_GetWindowSurface (window);
        // 获取 window 的 surface
        // SDL_SetRenderDrawColor (winRenderer,)
        // SDL_enderClear (winRenderer);
        // 清空 render
        winTexture = SDL_CreateTexture (winRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
        // 从 winRenderer 创建一个 texture 纹理

        SDL_FillRect (
            winSurface, nullptr,
            SDL_MapRGBA (
                winSurface -> format,
                XEGEGET_R (winBGColor),
                XEGEGET_G (winBGColor),
                XEGEGET_B (winBGColor),
                XEGEGET_A (winBGColor)
            )
        );
        // 填充背景色，默认为纯黑

        // SDL_RenderPresent (winRenderer);
        // 把 render 内容输出到 window 上面

        SDL_UpdateWindowSurface (window);
        // 更新 window
        // SDL_Delay (2000);

    }
    /*winRenderer = SDL_CreateRenderer (window, -1, SDL_RENDERER_ACCELERATED);
    winTexture = SDL_CreateTexture (winRenderer, SDL_PIXELFORMAT_RGBA32,
                                    SDL_TEXTUREACCESS_STREAMING,
                                    w, h);
    SDL_RenderClear (winRenderer);
    */


}
// 关闭窗口及相关一切
void closeGraph (void) {
	SDL_FreeSurface (winSurface);
	// 销毁绘画表面
	SDL_DestroyTexture (winTexture);
	// 销毁纹理
	SDL_DestroyRenderer (winRenderer);
	// 销毁渲染器
    IMG_Quit ();
    // 关闭 IMG
	SDL_DestroyWindow (window);
	// 销毁窗口
	SDL_Quit ();
	// 退出
	exit (1);

}
#endif